package cn.yudao.org.service.impl;

import cn.yudao.basic.service.impl.BaseServiceImpl;
import cn.yudao.basic.utils.PageList;
import cn.yudao.org.domain.Department;
import cn.yudao.org.domain.Employee;
import cn.yudao.org.mapper.DepartmentMapper;
import cn.yudao.org.query.DepartmentQuery;
import cn.yudao.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.lang.model.element.VariableElement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
* @Description: TODO 部门表的实现类
* @Author: Mr.Jia
* @Version: 1.0
* @Date:  2022/6/21 19:00
*/
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;





    /**
     * @Title: addDepartment
     * @Description: TODO 往部门表中添加数据
     * @Author: Mr.Jia
     * @Version: 1.0
     * @Date: 2022/6/21 18:57
     * @Parameters:
     * @Return
     */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirpath = "";
        if (department.getParent().getId() != null){
            Department byIdDepartment = departmentMapper.loadById(department.getParent().getId());
            if (byIdDepartment != null){
                dirpath = byIdDepartment.getDirPath() + "/" + department.getId();;
            }

        }else {
            dirpath = "/" + department.getId();
        }
            department.setDirPath(dirpath);
            departmentMapper.upadte(department);

    }

    /**
     * @Title: uptDepartment 根据id修改部门表中的数据
     * @Description: TODO
     * @Author: Mr.Jia
     * @Version: 1.0
     * @Date: 2022/6/21 18:58
     * @Parameters: Department部门的实体类
     * @Return
     */
    @Override
    @Transactional
    public void upadte(Department department) {
        String dirpath = "";
        if (department.getParent().getId() != null){
            Department byIdDepartment = departmentMapper.loadById(department.getParent().getId());
            if (byIdDepartment != null){
                dirpath = byIdDepartment.getDirPath() + "/" + department.getId();;
            }

        }else {
            dirpath = "/" + department.getId();
        }


        // 通过查询所有数据中prant_id=当前数据id的对象集合
        List<Department> byDirpathDepartment = departmentMapper.findByDirpathDepartment(department.getId());
        // 遍历
        for (Department department2 : byDirpathDepartment) {
            // 分割找到的数据的dirPath属性
            String[] split1 = department2.getDirPath().split("/" + department.getId());
            String dirpp = "";
            // 拼接为找到的新的dirpath地址
            dirpp = dirpath + split1[1];
            // 设置值
            department2.setDirPath(dirpp);

            // 给找到数据的Parent字段的id赋值为当前数据的Parent_id值，因为sql这样取值的
            Department department3 = new Department();
            department2.setParent(department3);
            department2.getParent().setId(department2.getParentId());

            // 给找到数据的Manager字段的id赋值为当前数据的Manager_id值，因为sql这样取值的
            Employee employee = new Employee();
            department2.setManager(employee);
            department2.getManager().setId(department2.getManagerId());

            // 修改查询到的数据
            departmentMapper.upadte(department2);
        }


//        String parentDirpath = "";
//        for (Department department1 : allDepartment) {
//            if (department.getId()==department1.getParent_id()){
//                parentDirpath = dirpath + "/" + department1.getId();
//                department1.setDirPath(parentDirpath);
//
//
//                Department department2 = new Department();
//                department1.setParent(department2);
//                department1.getParent().setId(department.getId());
//
//
//                Employee employee = new Employee();
//                department1.setManager(employee);
//                department1.getManager().setId(department1.getManager_id());
//
//                departmentMapper.uptDepartment(department1);
//            }
//        }



        department.setDirPath(dirpath);
        departmentMapper.upadte(department);
    }





    @Override
    public List<Department> loadTree() {

        List<Department> allDepartment = departmentMapper.LoadAll();

        HashMap<Long, Department> longDepartmentHashMap = new HashMap<>();

        for (Department department : allDepartment) {
            longDepartmentHashMap.put(department.getId(), department);
        }

        List<Department> tree = new ArrayList();

        for (Department department : allDepartment) {
            if (department.getParentId() == null) {
                tree.add(department);
            } else {
                Department department1 = longDepartmentHashMap.get(department.getParentId());
                if (department1 != null) {
                    department1.getChildren().add(department);
                }
            }
        }
        return tree;
    }

}
